package com.collect.tanzania.zhiUtils.zhiApkDownload;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;

import com.collect.tanzania.R;

/**
 * @author sunfusheng on 2018/2/17.
 */
public class ApkDialog {
    private ProgressDialog progressDialog;

    private OnClickDownloadDialogListener onClickDownloadDialogListener;


    public void showDownloadDialog(Context context, int progress) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(context);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setCancelable(false);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMax(100);
            progressDialog.setTitle(context.getResources().getString(R.string.downloading));
            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.waiting), new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which){
                    if (onClickDownloadDialogListener != null) {
                        onClickDownloadDialogListener.onClickBackgroundDownload(dialog);
                    }
                }
            });
//            progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener(){
//                @Override
//                public void onClick(DialogInterface dialog, int which){
//                    if (onClickDownloadDialogListener != null) {
//                        onClickDownloadDialogListener.onClickCancelDownload(dialog);
//                    }
//                }
//            });
            progressDialog.show();

            progressDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.theme_black_3));
            progressDialog.getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);

//            progressDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#9a9a9a"));
        }

        if (progressDialog.isShowing()) {
            progressDialog.setProgress(progress);
        }
    }

    public void dismissDownloadDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        progressDialog = null;
    }

    public void setOnClickDownloadDialogListener(OnClickDownloadDialogListener onClickDownloadDialogListener) {
        this.onClickDownloadDialogListener = onClickDownloadDialogListener;
    }

    public interface OnClickDownloadDialogListener {
//        void onClickDownload(DialogInterface dialog);

        void onClickBackgroundDownload(DialogInterface dialog);
//
//        void onClickCancelDownload(DialogInterface dialog);
    }

}
